﻿/**
 * jQuery EasyUI 1.5.1 --- 功能扩展 
 * TiddlerCJ
*/
(function($) {
	if ($.fn.datagrid){
	    $.fn.datagrid.defaults.pageSize = 20;//这里一定要用datagrid.defaults.pageSize，用pagination.defaults.pageSize一直不行
	    $.fn.datagrid.defaults.pageList = [10,20,30,40,50];//这里一定要有，不然上面的也不起效
	}
	
	/**
	 * tab方法扩展
	 * https://blog.csdn.net/qq_39578388/article/details/79030478
	 */
	$.extend($.fn.tabs.methods, {
	    getTabById: function(jq,id) {
	        var tabs = $.data(jq[0], 'tabs').tabs;
	        for(var i=0; i<tabs.length; i++){
	            var tab = tabs[i];
	            if (tab.panel('options').id == id){
	                return tab;
	            }
	        }
	        return null;
	    },
	    selectById:function(jq,id) {
	        return jq.each(function() {
	            var state = $.data(this, 'tabs');
	            var opts = state.options;
	            var tabs = state.tabs;
	            var selectHis = state.selectHis;
	            if (tabs.length == 0) {return;}
	            var panel = $(this).tabs('getTabById',id); // get the panel to be activated 
	            if (!panel){return}
	            var selected = $(this).tabs('getSelected');
	            if (selected){
	                if (panel[0] == selected[0]){return}
	                $(this).tabs('unselect',$(this).tabs('getTabIndex',selected));
	                if (!selected.panel('options').closed){return}
	            }
	            panel.panel('open');
	            var title = panel.panel('options').title;        // the panel title 
	            selectHis.push(title);        // push select history 
	            var tab = panel.panel('options').tab;        // get the tab object 
	            tab.addClass('tabs-selected');
	            // scroll the tab to center position if required. 
	            var wrap = $(this).find('>div.tabs-header>div.tabs-wrap');
	            var left = tab.position().left;
	            var right = left + tab.outerWidth();
	            if (left < 0 || right > wrap.width()){
	                var deltaX = left - (wrap.width()-tab.width()) / 2;
	                $(this).tabs('scrollBy', deltaX);
	            } else {
	                $(this).tabs('scrollBy', 0);
	            }
	            $(this).tabs('resize');
	            opts.onSelect.call(this, title, $(this).tabs('getTabIndex',panel));
	        });
	    },
	    existsById:function(jq,id){
	        return $(jq[0]).tabs('getTabById',id) != null;
	    }
	});
	
	$.extend(
			$.fn.dialog.methods,{
				openWin : function(jq, p) {
					var id = Math.round(Math.random()*1000) + '' ; 
					var html = "<div class=\"easyui-window\"></div>";
					var win = $(html);
					var $iframe = $('<iframe src="" frameborder="0" width="100%" height="100%"></iframe>');//.attr("id",id).attr("name",id);
					if (p && p.href) {
						$iframe.attr("src", p.href);
					}
					delete p.href;
					$iframe.appendTo(win);
					/*
					 *关闭时执行销毁操作 
					 */
					p = $.extend( p ,{ 
						onClose: function(){
							$(this).dialog('destroy');
						}	
					}) ; 
					win = win.dialog(p);
					win.iframeid = id;
					win.iframeWindow = $iframe.get(0).contentWindow; // 添加对子窗口window对象的引用
					$iframe.get(0).dialog = win;// 增加窗口对dialog对象的引用
					/*
					 * 可以在子窗口这样使用： var dialog = frameElement.dialog;
					 */
					return win;
				},
				/**
				 * 通过参数名称获取相应的属性
				 * 
				 * @param jq
				 * @param name
				 * @returns {Boolean}
				 */
				get : function(jq, name) {
					return jq.dialog('options')[name] || null;
				},
				closeCallback: function(jq,data){
					jq.data('callback', data).dialog('close');
				}
			});

		/**
		 * 扩展序列化方法
		 */
		$.fn.serializeObject = function() {
			var o = {}, a = this.serializeArray();
			$.each(a, function() {
				if (o[this.name] !== undefined) {
					if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; }
					o[this.name].push(this.value || '');
				} else { o[this.name] = this.value || ''; }
			});
			return o;
		};
		
		
		jQuery.extend({
			RMB: function(){
				if(arguments.length==0) return;
				var number = Math.round(arguments[0] * 100) / 100;
				number = number.toString(10).split(".");
				var a = number[0];
				if (a.length > 12) {
					return ""
				} else {
					var e = "零壹贰叁肆伍陆柒捌玖";
					var num1 = "";
					var len = a.length - 1;
					for (var i = 0; i <= len; i++) num1 += e.charAt(parseInt(a.charAt(i))) + [["元", "万", "亿"][Math.floor((len - i) / 4)], "拾", "佰", "仟"][(len - i) % 4];
					if (number.length == 2 && number[1] != "") {
						var a = number[1];
						for (var i = 0; i < a.length; i++) num1 += e.charAt(parseInt(a.charAt(i))) + ["角", "分"][i]
					}
					num1 = num1.replace(/零佰|零拾|零仟|零角/g, "零");
					num1 = num1.replace(/零{2,}/g, "零");
					num1 = num1.replace(/零(?=元|万|亿)/g, "");
					num1 = num1.replace(/亿万/, "亿");
					num1 = num1.replace(/^元零?/, "");
					if (num1 != "" && !/分$/.test(num1)) num1 += "整";
					return num1
				}
			}
		});
		
		
		/**对Date的扩展，将 Date 转化为指定格式的String
		*月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
		*年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
		*/
		Date.prototype.Format = function (fmt) {
		    var o = {
		        "M+": this.getMonth() + 1, //月份 
		        "d+": this.getDate(), //日 
		        "H+": this.getHours(), //小时 
		        "m+": this.getMinutes(), //分 
		        "s+": this.getSeconds(), //秒 
		        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		        "S": this.getMilliseconds() //毫秒 
		    };
		    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		    for (var k in o)
		    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		    return fmt;
		}
		/**
		 * 确认扩展
		 */
		jQuery.extend({
			fnConfirm:function(msg,fn){
				msg = msg || '您确认执行当前操作吗？' ;
				$.messager.confirm('确认对话框', msg, function(r){
					if (r && fn){
						fn();
					}
				});
			}
		});
	
	/**
	 * jQuery EasyUI 1.5.1 --- 功能扩展 
	 * 新增 validatebox 校验规则 
	 * TiddlerCJ
	*/
    $.extend($.fn.validatebox.defaults.rules, {
    	email:{
			validator: function(value){
				return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
			},
			message: "请输入有效的邮箱地址"
		},
		url:{
			validator: function(value){
				return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
			},
			message: "请输入有效的URL地址"
		},
    	minLen:{
    		validator:function(value,param) {
    			return value.length>=param[0];
    		},
    		message:'请最少输入{0}个字符'
		},
		maxLen:{
			validator:function(value,param) {
				return value.length<=param[0];
			},
			message:'最多输入{0}个字符'
		},
		mobile:{
			validator: function(value){
				var regex = /^(1[34578]{1}[0-9]{1}[\d]{8})$/;
				return regex.test(value);
			},
			message: "请输入正确手机号码"
		},
		mobiles:{
			validator: function(value){
				var regex = /^(1[34578]{1}[0-9]{1}[\d]{8})$/;
				var flag = true;
				value = value || "";
				var mobiles = value.split(",");
				 $.each(mobiles, function (index, mobile) {
					 if(mobile){
						 flag = flag && regex.test(mobile);
						 if(flag ==false) return false;
					 }else{
						flag = false;
						return false;
					 }
	             });
				return flag;
			},
			message: "请输入正确手机号码"
		},
		phone:{
			 validator:function(value) {
				 return/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
			 },
			 message:'请输入正确电话如: 027-88888888'
		},
    	digits: {  
            validator: function(value, param) {  
                return /^([0-9]+)$/.test(value);  
            },  
            message: '请输入正整数'  
        },
        idcard:{
        	 validator:function(value) {
        		 return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
        	 },
        	 message:'请输入正确身份证号码'
        },
        qq:{
        	 validator:function(value) {
        		 return /^[1-9]\d{4,9}$/i.test(value);
        	 },
        	 message:'请输入正确QQ号码'
        },
        cn:{
        	 validator:function(value) {
        	 	return /^[\u0391-\uFFE5]+$/i.test(value);
        	 },
        	 message:'请输入中文'
        },
        en:{
        	 validator:function(value) {
        	 	return /^[A-Za-z]+$/i.test(value);
        	 },
        	 message:'请输入英文'
        },
        zip:{
        	 validator:function(value) {
        		 return/^[1-9]\d{5}$/i.test(value);
        	 },
        	 message:'请输入正确邮政编码'
        },
        ip:{
        	 validator:function(value) {
        		 var reg =  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/ ;    
        	     return reg.test(value);   
        	 },
        	 message:'请输入正确IP地址'
        },
        percent:{
        	 validator:function(value) {
        		 return /^(100|[1-9]?\d(\.\d\d?\d?)?)%$|0$/.test(value);
        	 },
        	 message:'请输入正确的百分数,如5%'
        },
        ///^(100|[1-9]?\d(\.\d\d?\d?)?)%$|0$/
        //^(100|[1-9]\d|\d)(.\d{1,2})?%$
        carNo:{
        	 validator:function(value){
        	 return /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/.test(value); 
        	 },
        	 message:'请输入正确号牌如: 鄂D12350'
        },
        equalTo:{
			isNull: true,	//表示即便没有输入值，也要执行验证
			validator: function(value, param){
				return value == $(document.getElementById(param[0])).val();
			},
			message: "两次输入的字符不一致"
		},
		decimals: {
			validator: function(value, param){
				var fex = "+";
				if(param&&param.length>0){
					fex = "{"+param[0]+"}";
				}
				var reg = new RegExp("^[+-]?((\\d+\\.\\d"+fex+")|\\d*)$","g");
				return reg.test(value);
			},
			message: "数字保留{0}位小数"
		},
		remote:{
			validator: function(value,param){
				var len = param.length;
				if(2<len && value===param[2]) return true;
				var p = {};
				p[param[1]] = value;
				p.original = len>2 ? param[2] : '';
				p.paramName = param[1];
				var res = $.ajax({ url:param[0], dataType:"json", data:p, async:false, cache:false, type:"post" }).responseText;
				return res=="true";
			},
			message:"已存在,请更换"
		}
    });  
})(jQuery);  